<template>
  <el-dialog v-model="show" title="素材选择" width="800" class="rounded-[15px]" :destroy-on-close="true">
    <div>
      <list
        :type="initParams.type"
        :total="sucaiInfo.total_count"
        :list="sucaiInfo.item"
        v-bind="$attrs"
        @confirm="show = false"
      ></list>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { draftList, suCaiList } from "@/api/modules/wechat";
import list from "./list.vue";
const show = ref(false);

const sucaiInfo = ref({
  item: [],
  total_count: 0
});

const initParams = reactive<{ offset: number; count: number; type: "news" | "video" | "image" | "voice" }>({
  offset: 0,
  count: 10,
  type: "image"
});

// 获取图文消息
const getNews = async () => {
  const { data } = await draftList({ offset: initParams.offset, count: initParams.count, no_content: 0 });
  sucaiInfo.value = data as any;
};

// 获取素材
const getSuCai = async (type: "news" | "video" | "image" | "voice") => {
  const { data } = await suCaiList({ offset: initParams.offset, count: initParams.count, type });
  initParams.type = type;
  sucaiInfo.value = data as any;
};

const open = (type: "news" | "video" | "image" | "voice" | "text") => {
  if (type === "news") {
    getNews();
  } else if (type === "text") {
    return;
  } else {
    getSuCai(type);
  }
  initParams.type = type;
  show.value = true;
};

defineExpose({ open });
</script>

<style></style>
